<?php
/**
 * 广告模块
 * @author zlc
 * @version 1.0
 * @date: 2020/8/20
 * @time: 11:28
 */
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Ad;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class AdController extends Controller
{

    /*
     * 列表页加载
     */
    public function index(){

        return view('admin.ad.index');
    }

    /*
     * 列表页面数据
     */
    public function listData(Request $request){
        $model = Ad::query();
        if ($request->get('title')) {
            $model->where('title', 'like', '%' . $request->get('title') . '%');
        }
        if ($request->get('keyword')) {
            $model->where('keyword', 'like', '%' . $request->get('keyword') . '%');
        }
        if ($request->get('status')) {
            $model->where('status', '=', $request->get('status'));
        }
        if ($request->get('category_id')) {
            $model->where('category_id', '=', $request->get('category_id'));
        }
        $num = $request->get('limit', self::ADMIN_PAGE_NUM);
        $result = $model
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['start_time'] = date('Y-m-d H:i:s',$val['start_time']);
            $result['data'][$key]['end_time'] = date('Y-m-d H:i:s',$val['end_time']);
            $result['data'][$key]['status_name'] = config('status.ad_status.'.$val['status']);
        }
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $result['total'],
            'data' => $result['data']
        ];
        return response()->json($data);

    }

    /*
     * 添加页面加载
     */
    public function createForm(){


        return view('admin.ad.create');
    }

    /*
     * 添加数据保存
     */
    public function store(Request $request){

        $data = $request->except(['yxq_time','file']);
        if ($data['ad_title']) {
            $ini['ad_title'] = $data['ad_title'];
            $ini['status'] = 1;
            $isFlag = Ad::where($ini)->value('id');
            if ($isFlag) {
                return redirect()->back()->withInput()->with(['error' => '广告名称已存在']);
            }
        }
        $data['area_name'] = $data['province'].'-'.$data['city'].'-'.$data['county'];
        $yxq_time = $request->input('yxq_time');
        if($yxq_time){
            $yxq_data = explode(' - ',$yxq_time);
            $data['start_time']  = strtotime($yxq_data[0]);
            $data['end_time']  = strtotime($yxq_data[1]);
        }
        $res = Ad::create($data);
        if ($res) {
            return redirect()->to(route('admin.ad.index'))
                ->with(['status' => '添加成功']);
        }
        return redirect()->to(route('admin.ad.create.form'))
            ->with(['status' => '添加失败']);
    }

    /*
     * 编辑页面加载
     */
    public function editForm($id){
        $data = Ad::where(['id'=>$id])->first();
        $data['yxq_time'] = date('Y-m-d H:i:s',$data['start_time'])." - ".date('Y-m-d H:i:s',$data['end_time']);
        return view('admin.ad.edit')->with('data',$data);
    }

    /*
     * 编辑保存
     */
    public function update(Request $request){
        $id = $request->get('id');
        $data = $request->except(['yxq_time','file']);
        if($data['ad_title']){
            $title = Ad::where(['id'=>$id])->value('ad_title');
            if($title != $data['ad_title']){
                $ini['ad_title'] = $data['ad_title'];
                $ini['status'] = 1;
                $isFlag = Ad::where($ini)->value('id');
                if ($isFlag) {
                    return redirect()->back()->withInput()->with(['error' => '广告名称已存在']);
                }
            }
        }
        $yxq_time = $request->input('yxq_time');
        if($yxq_time){
            $yxq_data = explode(' - ',$yxq_time);
            $data['start_time']  = strtotime($yxq_data[0]);
            $data['end_time']  = strtotime($yxq_data[1]);
        }
        $modelObj = Ad::findOrFail($id);
        $data['area_name'] = $data['province'].'-'.$data['city'].'-'.$data['county'];
        if($modelObj->update($data)){
            return redirect()->to(route('admin.ad.index'))->with(['status' => '更新成功']);
        }else{
            return redirect()->back()->withInput()->with(['error' => '更新失败']);
        }

    }

    /*
     * 排序
     */
    public function sort(Request $request){
        $data = $request->all();
        $id = $data['id'];
        $sortData['sort'] = $data['val'];
        $res = Ad::where(['id' => $id])->update($sortData);
        if ($res) {
            $ini['code'] = self::RESPONSE_CODE_SUCCESS;
            $ini['msg'] = '更新成功';
        } else {
            $ini['code'] = self::RESPONSE_CODE_ERROR;
            $ini['msg'] = '更新失败';
        }
        return response()->json($ini);
    }

    /*
     * 修改状态值
     */
    public function status(Request $request){
        $id = $request->input('id');
        $status = $request->input('status');
        if($status == 1){
            $saveData['status'] = 2;
        }else{
            $saveData['status'] = 1;
        }
        $adObj = Ad::findOrFail($id);
        if($adObj->update($saveData)){
            return response()->json([
                'code' => self::RESPONSE_CODE_SUCCESS,
                'msg' => '更新成功',
            ]);
        }else{
            return response()->json([
                'code' => self::RESPONSE_CODE_ERROR,
                'msg' => '更新失败!',
            ]);
        }
    }

    /*
     * 删除
     */
    public function delete(Request $request){
        if (!$request->input('id')) {
            return response()->json([
                'code' => self::RESPONSE_CODE_ERROR,
                'msg' => '删除失败!'
            ]);
        }
        $id = $request->input('id');
        $result =Ad::destroy($id);
        if ($result) {
            return response()->json([
                'code' => self::RESPONSE_CODE_SUCCESS,
                'msg' => '删除成功!'
            ]);
        }
        return response()->json([
            'code' => self::RESPONSE_CODE_ERROR,
            'msg' => '删除失败!'
        ]);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request){
        $type = $request->input('type',1);
        $idArr = $request->input('myArray');
        if($type == 1){ //禁用
            $saveData['status'] = 2;
        }elseif ($type == 2){ //启用
            $saveData['status'] = 1;
        }  else{//删除
        }
        if($type == 1 || $type == 2){
            $res = Ad::whereIn('id',$idArr)->update($saveData);
        }else{
            $res = Ad::destroy($idArr);
        }
        if($res){
            $ini['code'] = self::RESPONSE_CODE_SUCCESS;
            $ini['msg']   = '更新成功';
        }else{
            $ini['code'] = self::RESPONSE_CODE_ERROR;
            $ini['msg']   = '更新失败';
        }
        return response()->json($ini);
    }

}